<?php

namespace App\Http\Controllers;

use App\Http\Resources\FactoryLikeCollection;
use App\Models\Factory;
use App\Models\FactoryLike;
use Illuminate\Http\Request;

class LikeController extends Controller
{
    public function like(Request $request)
    {
        $factoryId = $request->input('factory_id');
        $user = $request->user();
        $factory = Factory::with('tag')->findOrFail($factoryId);
        $factoryLike = FactoryLike::where('factory_id', $factoryId)->where('user_id', $user->id)->first();
        if ($factoryLike) {

            return [
                'code' => 200,
                'message' => '已经收藏',
                'data' => ''
            ];
        } else {
            $factoryLike = new FactoryLike();
            $factoryLike->factory_id = $factoryId;
            $factoryLike->user_id = $user->id;
            $factoryLike->model = $factory->toArray();
            $factoryLike->save();
            return [
                'code' => 200,
                'message' => '收藏成功',
                'data' => ''
            ];
        }
    }

    public function unlike(Request $request)
    {
        $factoryId = $request->input('factory_id');
        $is_all = $request->input('is_all');

        $user = $request->user();
        if (is_string($factoryId)) {
            $factoryId = explode(',', $factoryId);
        }

        $factoryLike = FactoryLike::when(!$is_all, function ($query) use ($factoryId, $user) {
            $query->whereIn('factory_id', $factoryId)
                ->where('user_id', $user->id);
        })->get();
        foreach ($factoryLike as $item) {
            $item->delete();
        }
        return [
            'code' => 200,
            'message' => '取消收藏成功',
            'data' => ''
        ];
    }

    public function isLike(Request $request)
    {
        $factoryId = $request->input('factory_id');
        $user = $request->user();
        if (!$user) {
            return [
                'code' => 200,
                'message' => '未收藏',
                'data' => 0
            ];
        }

        $factory = Factory::find($factoryId);
        if (!$factory) {
            return [
                'code' => 200,
                'message' => '未收藏',
                'data' => 0
            ];
        }
        $factoryLike = FactoryLike::where('factory_id', $factoryId)->where('user_id', $user->id)->first();
        if ($factoryLike) {
            return [
                'code' => 200,
                'message' => '已收藏',
                'data' => 1
            ];
        }
        return [
            'code' => 200,
            'message' => '未收藏',
            'data' => 0
        ];
    }

    public function likeList(Request $request)
    {
        $user = $request->user();
        $pageSize = $request->input('page_size');
        $factoryLike = FactoryLike::where('user_id', $user->id)
            ->orderBy('id', 'desc')
            ->paginate($pageSize);
        //  return $factoryLike;
        return new FactoryLikeCollection($factoryLike);
    }
}
